﻿using System;
using System.Linq;
using PersonalAccoutingProgram.Data;
using System.Windows.Forms;

namespace PersonalAccoutingProgram.View
{
    public partial class EditExpenses : Form
    { 
        private string username;

        public EditExpenses(string username)
        {
            InitializeComponent();
            this.username = username;
            InitializeGridView(username);
        }

        private void InitializeGridView(string username)
        {
            ExpenseDataClassesDataContext expensesDC = new ExpenseDataClassesDataContext();
            var expensesView =
            from currentExpenses in expensesDC.GetTable<ExpensesView>()
            where username == currentExpenses.Username
            select
            new
            {
                ID = currentExpenses.ExpensesID,
                Category = currentExpenses.CategoryName,
                Date = currentExpenses.ExpenseDate,
                Amount = currentExpenses.ExpenseAmount
            };

            dataGridView.DataSource = expensesView;
            dataGridView.Columns[0].Visible = false;
        }

        private void EditExpenses_Load(object sender, EventArgs e)
        {
        }

        private void dataGridView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        { 
            int gridID = (int)dataGridView.SelectedRows[0].Cells[0].Value;
            string gridCategory = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
            string gridDate = dataGridView.SelectedRows[0].Cells[2].Value.ToString();
            string gridAmount = dataGridView.SelectedRows[0].Cells[3].Value.ToString();
            gridCategory = gridCategory.Trim();
            gridDate = gridDate.Trim();
            gridAmount = gridAmount.Trim();
            EditExpensesForm newEditForm =
                new EditExpensesForm(gridID, gridDate, gridAmount, gridCategory);
            newEditForm.ShowDialog();
            InitializeGridView(username);
        }
    }
}